package com.woniu.communitymanagement.service.impl;

import com.woniu.communitymanagement.entity.HttpResult;
import com.woniu.communitymanagement.entity.PropertyInfo;
import com.woniu.communitymanagement.mapper.PropertyInfoMapper;
import com.woniu.communitymanagement.service.IPropertyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PropertyInfoServiceImpl implements IPropertyInfoService {

    @Autowired(required = false)
    private PropertyInfoMapper propertyInfoMapper;

    @Override
    public HttpResult getProInfoList(String numbers, String status, int pageIndex, int pageSize) {
        List<PropertyInfo> propertyInfos = propertyInfoMapper.selectAll(numbers, status, (pageIndex-1)*pageSize, pageSize);
        int count = propertyInfoMapper.count(numbers, status);
        HttpResult result = null;
        if (propertyInfos != null && propertyInfos.size()>0){
            result = new HttpResult(propertyInfos,count,200,null);
        }else{
            result = new HttpResult(null,0,500,"没有更多数据");
        }
        return result;
    }

    @Override
    public HttpResult addProInfo(PropertyInfo propertyInfo) {
        int count = propertyInfoMapper.insert(propertyInfo);
        HttpResult result = null;
        if (count>0){
            result = new HttpResult(null,0,200,"新增成功");
        }else{
            result = new HttpResult(null,0,500,"新增失败");
        }
        return result;
    }

    @Override
    public HttpResult removeProInfo(int id) {
        int count = propertyInfoMapper.deleteById(id);
        HttpResult result = null;
        if (count>0){
            result = new HttpResult(null,0,200,"删除成功");
        }else{
            result = new HttpResult(null,0,500,"删除失败");
        }
        return result;
    }

    @Override
    public HttpResult updateProInfo(PropertyInfo propertyInfo) {
        int count = propertyInfoMapper.updateById(propertyInfo);
        HttpResult result = null;
        if (count>0){
            result = new HttpResult(null,0,200,"更新成功");
        }else{
            result = new HttpResult(null,0,500,"更新失败");
        }
        return result;
    }

    @Override
    public HttpResult getInfoById(int id) {
        PropertyInfo propertyInfo = propertyInfoMapper.selectById(id);
        HttpResult result = null;
        if (propertyInfo != null){
            result = new HttpResult(propertyInfo,0,200,null);
        }else{
            result = new HttpResult(null,0,500,"没有更多数据");
        }
        return result;
    }

}
